package com.climber.android.commonsdk.helper

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.fragment.app.Fragment
import com.climber.android.commonsdk.R
import java.io.Serializable


/**
 * Created by mikes on 2019/1/29.
 */
inline fun <reified F : Fragment> Context.newFragment(vararg args: Pair<String, Any>): F {
    val bundle = Bundle()
    args.let {
        for (arg in args) {
            when (val second = arg.second) {
                is Int -> bundle.putInt(arg.first, second)
                is Long -> bundle.putLong(arg.first, second)
                is Double -> bundle.putDouble(arg.first, second)
                is Boolean -> bundle.putBoolean(arg.first, second)
                is String -> bundle.putString(arg.first, second)
                is Serializable -> bundle.putSerializable(arg.first, second)
                is Parcelable -> bundle.putParcelable(arg.first, second)
                else -> {}
            }
        }
    }
    return Fragment.instantiate(this, F::class.java.name, bundle) as F
}

fun Fragment.openActivity(intent : Intent) {
    startActivity(intent)
    activity?.overridePendingTransition(R.anim.cbase__slide_in_from_right, R.anim.cbase__slide_out_to_left)
}

fun Fragment.openActivityForResult(intent : Intent, requestCode : Int) {
    startActivityForResult(intent, requestCode)
    activity?.overridePendingTransition(R.anim.cbase__slide_in_from_right, R.anim.cbase__slide_out_to_left)
}